package fr.xgouchet.wedding.client.service;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.datepicker.client.DateBox;

import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.i18n.Messages;
import fr.xgouchet.wedding.shared.model.NavLink;
import fr.xgouchet.wedding.shared.model.ProjectSettings;
import fr.xgouchet.wedding.shared.util.Encryption;

/**
 * Service to retrieve the settings of the user's wedding
 * 
 * @author x.gouchet
 * 
 */
public class WebService {

	/**
	 * Private Default Constructor
	 */
	private WebService() {
		SETTINGS = new ProjectSettings();
		SETTINGS.setCurrentPsswd(Encryption.md5("bigbang"));
		SETTINGS.setBrideFirstName("Adam");
		SETTINGS.setGroomFirstName("Eve");
		SETTINGS.setUserMail("god@eden.org");
		SETTINGS.setWeddingDate((new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("dd/MM/yyyy")).parse(null, "06/08/2011", false)));

	}

	/**
	 * Fetch this project settings (info that may be used in all the app)
	 * 
	 * @param asyncCallback
	 *            the callback to which the data will be transmitted
	 */
	public static void fetchSettings(
			AsyncCallback<ProjectSettings> asyncCallback) {
		asyncCallback.onSuccess(SETTINGS);
	}

	/**
	 * @return the cached settings
	 */
	public static ProjectSettings getSettings() {
		return SETTINGS;
	}

	/**
	 * Saves the settings on server
	 * 
	 * @param settings
	 *            the new settings value
	 * @param asyncCallback
	 *            the callback to which the data will be transmitted
	 */
	public static void saveSettings(ProjectSettings settings,
			AsyncCallback<ProjectSettings> asyncCallback) {
		if (settings.isChangingPassword()) {
			if (settings.getCurrentPsswd().equals(SETTINGS.getCurrentPsswd())) {
				settings.setChangePassword(false);
				settings.setCurrentPsswd(settings.getNewPsswd());
				settings.setNewPsswd("");
				settings.setNewPsswdCheck("");
				SETTINGS = settings;
				asyncCallback.onSuccess(settings);
			} else {
				asyncCallback.onFailure(new Throwable(Messages.I18N
						.passwordInvalid()));
			}
		} else {
			settings.setChangePassword(false);
			settings.setCurrentPsswd("");
			settings.setNewPsswd("");
			settings.setNewPsswdCheck("");
			SETTINGS = settings;
			asyncCallback.onSuccess(settings);
		}
	}

	/**
	 * @return the list of links
	 */
	public static ArrayList<NavLink> getLinksDetails() {
		if (NAV_LINKS == null) {
			NAV_LINKS = new ArrayList<NavLink>();

			NAV_LINKS.add(new NavLink("home", Constants.I18N.linkHome(),
					Constants.I18N.categoryOrganisation()));
			NAV_LINKS.add(new NavLink("settings",
					Constants.I18N.linkSettings(), Constants.I18N
							.categoryOrganisation()));
			NAV_LINKS.add(new NavLink("agenda", Constants.I18N.linkAgenda(),
					Constants.I18N.categoryOrganisation()));
			NAV_LINKS.add(new NavLink("budget", Constants.I18N.linkBudget(),
					Constants.I18N.categoryOrganisation()));
			NAV_LINKS.add(new NavLink("procedures", Constants.I18N
					.linkprocedures(), Constants.I18N.categoryOrganisation()));
			NAV_LINKS.add(new NavLink("gifts", Constants.I18N.linkGifts(),
					Constants.I18N.categoryOrganisation()));

			NAV_LINKS.add(new NavLink("invitations", Constants.I18N
					.linkInvitation(), Constants.I18N.categoryGuests()));
			NAV_LINKS.add(new NavLink("guests-list", Constants.I18N
					.linkGuests(), Constants.I18N.categoryGuests()));
			NAV_LINKS.add(new NavLink("guests-seating", Constants.I18N
					.linkSeating(), Constants.I18N.categoryGuests()));

			NAV_LINKS.add(new NavLink("schedule",
					Constants.I18N.linkSchedule(), Constants.I18N
							.categoryWedding()));
			NAV_LINKS.add(new NavLink("ceremony",
					Constants.I18N.linkCeremony(), Constants.I18N
							.categoryWedding()));
			NAV_LINKS.add(new NavLink("cocktail",
					Constants.I18N.linkCocktail(), Constants.I18N
							.categoryWedding()));
			NAV_LINKS.add(new NavLink("reception", Constants.I18N
					.linkReception(), Constants.I18N.categoryWedding()));

			/*
			 * <li>Prestataires</li> <li>Deco</li> <li>Habillement</li>
			 * <li>Alliances</li> <li>Dinner (menu)</li>
			 */
		}
		return NAV_LINKS;
	}

	/** */
	@SuppressWarnings("unused")
	private static void buildDataBase() {
		// Create Cache
	}

	/**
	 * @param map
	 *            map
	 * @return id
	 */
	@SuppressWarnings("unused")
	private static String getValidID(HashMap<String, ?> map) {
		String id = "";
		int i = 0;
		do {
			id = "" + i;
			++i;
		} while (map.containsKey(id));
		return id;
	}

	/** */
	private static ProjectSettings SETTINGS;

	/** */
	private static ArrayList<NavLink> NAV_LINKS;

	/** */
	@SuppressWarnings("unused")
	private static WebService SERVICE = new WebService();
}
